package com.ruyuan.seckilling.common;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * @author zhonghuashishan
 */
public final class FilePathUtils {
    public static final String BASE_DIR;

    static {
        String osName = System.getProperty("os.name");
        if (StringUtils.startsWithIgnoreCase(osName, "win")) {
            BASE_DIR = "E:\\ruyuan-seckilling-test\\";
        } else {
            BASE_DIR = "/ruyuan-seckilling-test/";
        }
    }

    public static String generateVisitUrl(String fileName) {
        String filePath = generateFilePath(fileName);
        filePath = StringUtils.replace(filePath, BASE_DIR, "");
        filePath = StringUtils.replace(filePath, File.separator, "/");
        return "/" + filePath;
    }

    public static String generateFilePath(String fileName) {
        // 计算fileName的hashcode
        int hashCode = Math.abs(fileName.hashCode());
        // 分成100个目录看这个文件应该放到那个目录
        int subDir = hashCode % 100;
        // baseDir、subDir、fileName拼接起来就是最终文件的路径
        return BASE_DIR + subDir + File.separator + fileName;
    }



    public static String getParentDir(String filePath) {
        return StringUtils.substringBeforeLast(filePath, File.separator);
    }
}
